package com.wanjia.estate.service.bdservice.bdcdjxxcxjgzmservice;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.wanjia.estate.mapper.GyfsMapper;
import com.wanjia.estate.pojo.entity.Bdcdjxxjgcx;
import com.wanjia.estate.pojo.entity.Details;
import com.wanjia.estate.pojo.httpservice.GetToken;
import com.wanjia.estate.pojo.httpservice.Hydjxxhygr;
import com.wanjia.estate.pojo.httpservice.Mzbhydjxx;
import com.wanjia.estate.service.httpservice.BdcHyZkHttpService;
import com.wanjia.estate.service.httpservice.PropertyHttpService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

@Slf4j
@Service
public class BdcDataService {

    @Autowired
    BdcHyZkHttpService bdcHyZkHttpService;

    @Setter(onMethod_ = @Autowired)
    private PropertyHttpService service;

    @Autowired
    GyfsMapper gyfsMapper;

    @Autowired
    SearchHomeService searchHomeService;

    @Autowired
    EnquiryOfMinorsService enquiryOfMinorsService;

    public List<Bdcdjxxjgcx> BdcDataIntegration(Details detail) {
        List<Bdcdjxxjgcx> bdcdjxxjgcxList = new ArrayList<>();

        //获取不动产的token
        //GetToken.Response token = bdcHyZkHttpService.token(new GetToken.Request());

        //查询本人名下房产
        List<Bdcdjxxjgcx> bdcdjxxjgcxList1 = searchHomeService.SearchHome(detail);
        log.info("本人名下房产{}", bdcdjxxjgcxList1);

        //查询婚姻状况
        Mzbhydjxx.Request request = new Mzbhydjxx.Request();
        Mzbhydjxx.RequestData data = new Mzbhydjxx.RequestData();
        data.setCert_num(detail.getIdcardno());
        data.setName(detail.getName());
        request.setData(CollUtil.newArrayList(data));
        Mzbhydjxx.Response mzbhydjxx = bdcHyZkHttpService.mzbhydjxx(request);
        List<Mzbhydjxx.Row> rows = mzbhydjxx.getData().getRows();


        log.info("结婚信息{}", rows);
        if (rows.isEmpty()) {
            log.info("未结婚");
            bdcdjxxjgcxList.addAll(bdcdjxxjgcxList1);
        }
        //获取result，解析拿到结婚、离婚、未婚
        for (Mzbhydjxx.Row row : rows) {
            log.info("结婚与否{}", row.getOp_type());
            if (row.getOp_type().equals("INR")) {
                log.info("未结婚");
                bdcdjxxjgcxList.addAll(bdcdjxxjgcxList1);
            } else if (row.getOp_type().equals("IA")) {
                log.info("已结婚");
                if (detail.getIdcardno().equals(row.getCert_num_man())) {
                    //权利人为男士 配偶为女士
                    Details details1 = new Details();
                    details1.setIdcardno(row.getCert_num_woman());
                    details1.setName(row.getName_woman());
                    //查询配偶名下房产
                    List<Bdcdjxxjgcx> bdcdjxxjgcxList2 = searchHomeService.SearchHome(details1);
                    log.info("配偶名下房产{}", bdcdjxxjgcxList2);
                    //比对与权利人的房产信息
                    merge(bdcdjxxjgcxList1, bdcdjxxjgcxList2, Bdcdjxxjgcx::getCqzh);
                    bdcdjxxjgcxList.addAll(bdcdjxxjgcxList1);
                } else {
                    //权利人为女士 配偶为男士
                    Details details1 = new Details();
                    details1.setIdcardno(row.getCert_num_man());
                    details1.setName(row.getName_man());
                    //查询配偶名下房产
                    List<Bdcdjxxjgcx> bdcdjxxjgcxList2 = searchHomeService.SearchHome(details1);
                    log.info("配偶名下房产{}", bdcdjxxjgcxList2);
                    //比对与权利人的房产信息
                    merge(bdcdjxxjgcxList1, bdcdjxxjgcxList2, Bdcdjxxjgcx::getCqzh);
                    bdcdjxxjgcxList.addAll(bdcdjxxjgcxList1);
                }

                //查询名下未成年子女
                List<Details> details = enquiryOfMinorsService.EnquiryOfMinors(detail);
                log.info("未成年子女信息{}", details);
                for (Details detail1 : details) {
                    //查询名下未成年子女房产
                    List<Bdcdjxxjgcx> bdcdjxxjgcxList3 = searchHomeService.SearchHome(detail1);
                    //比对与权利人的房产信息
                    //merge(bdcdjxxjgcxList1,bdcdjxxjgcxList3,Bdcdjxxjgcx::getCqzh);
                    bdcdjxxjgcxList.addAll(bdcdjxxjgcxList3);
                }
            } else if (row.getOp_type().equals("IB")) {
                log.info("离婚");
                //查询名下未成年子女
                List<Details> details = enquiryOfMinorsService.EnquiryOfMinors(detail);
                for (Details detail1 : details) {
                    List<Bdcdjxxjgcx> bdcdjxxjgcxList4 = searchHomeService.SearchHome(detail1);
                    //比对与权利人的房产信息
                    // merge(bdcdjxxjgcxList1,bdcdjxxjgcxList4,Bdcdjxxjgcx::getCqzh);
                    bdcdjxxjgcxList.addAll(bdcdjxxjgcxList4);
                }
            }
        }


        return bdcdjxxjgcxList;
    }

    private static <T, K> void merge(List<T> list1, List<T> list2, Function<T, K> function) {
        if (list1 == null || CollUtil.isEmpty(list2)) {
            return;
        }

        Set<K> set = new HashSet<>();
        list1.stream().map(function).forEach(set::add);

        list2.forEach(t -> {
            K k = function.apply(t);
            if (set.contains(k)) {
                return;
            }
            set.add(k);
            list1.add(t);
        });
    }
}
